<?php

namespace App\Admin\Controllers;

use App\Models\Product;
use App\Models\TradeRecommend;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class TradeRecommendController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('成果展示');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('成果展示');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('成果展示');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(TradeRecommend::class, function (Grid $grid) {

            $grid->model()->whereHas('product', function ($query) {

            });

            $grid->id('ID')->sortable();
            $grid->disableExport();

            $grid->column('product_id', '书名')->display(function ($value) {
                $product = Product::select('id', 'name', 'uuid')->find($value);
                return $product ? $product->name : '<p style="color: red;">书籍不存在</p>';
            });
            $grid->column('product_link', '链接')->display(function () {
                $product = Product::select('id', 'name', 'uuid')->find($this->product_id);
                if ($product)
                    return '<a href="' . route('productInfo', ['uuid' => $product->uuid]) . '" target="_blank">查看</a>';
                return '查看';
            });
            $grid->created_at('上线时间');

            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
                $filter->where(function ($query) {
                    $query->whereHas('product', function ($q) {
                        $input = $this->input;
                        $q->select('id', 'name');
                        $q->where('name', $input);
                    });
                }, '书籍名称');
                $filter->between('created_at', '上线时间')->datetime();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(TradeRecommend::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->select('product_id', '关联书籍')
                ->options(function ($productId) {
                    $product = Product::select('id', 'name')->where('auditing_status', 'online')->find($productId);
                    if ($product)
                        return [$product->id => $product->name];
                })
                ->ajax('/product/list')
                ->help('输入产品名字进行查找并选择')
                ->rules('required');

            $form->display('created_at', '添加时间');
            $form->display('updated_at', '更新时间');
        });
    }
}
